<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>File: ardto.rb</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



  <div id="fileHeader">
    <h1>ardto.rb</h1>
    <table class="header-table">
    <tr class="top-aligned-row">
      <td><strong>Path:</strong></td>
      <td>dto/ardto.rb
      </td>
    </tr>
    <tr class="top-aligned-row">
      <td><strong>Last Update:</strong></td>
      <td>Tue Oct 16 00:25:32 +0200 2007</td>
    </tr>
    </table>
  </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <h1>Data Transfer Objects and Active Record</h1>
<p>
This file is part of the <a href="../../classes/ETL.html">ETL</a> package
and contains support structures to map between DTOs and <a
href="../../classes/ActiveRecord.html">ActiveRecord</a>.
</p>
<h2>Object-Relational Mapping and JSON</h2>
<p>
This file enriches <a
href="../../classes/ActiveRecord.html">ActiveRecord</a> (by mixin some code
into <tt>ActiveRecord::Base</tt> ) to allow to-and-from conversion between
<a href="../../classes/ActiveRecord.html">ActiveRecord</a> entities and
JSON objects. Refer to the <a
href="../../classes/ActiveRecord.html">ActiveRecord</a> documentation for
further info.
</p>
<p>
Conversion from AR to JSON allows relationship traversal via the
<tt>json_include</tt> macro, as in the following example:
</p>
<pre>
  class Order &lt; ActiveRecord::Base
    has_many :items
    json_include :items
  end
  ...
  require 'json'
  o = Order.find(10)
  s = JSON.generate(o)
</pre>
<p>
Conversion from JSON to AR tries loading the serialized entities from the
database (via <tt>find</tt> method, using the <tt>id</tt> the entity had at
serialization time). If the load fails, the entity is created as new
(discarding the previous id). This behavior has some side-effects:
</p>
<ul>
<li>if an entity is deleted between serialization and restore, on <tt>save</tt>
will be recreated with a different id,

</li>
<li>if an entity changes foreign key between serialization and restore, on
<tt>save</tt> will be reassigned to the original parent.

</li>
</ul>
<h2>Author</h2>
<p>
Riccardo Govoni [battlehorse@gmail.com]
</p>
<h2>Copyright</h2>
<p>
Copyright(c) 2007 - bayes-swarm project. Licensed under the Apache2
License.
</p>

    </div>

    <div id="requires-list">
      <h3 class="section-bar">Required files</h3>

      <div class="name-list">
      json&nbsp;&nbsp;
      </div>
    </div>

   </div>


  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>